<?php

namespace common\modules\article\models\backend;

use common\helpers\Html;
use common\widgets\grid\ActionColumn;
use common\widgets\GridView;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;

/**
 * SiteSearch represents the model behind the search form about `common\modules\article\models\backend\Site`.
 */
class SiteSearch extends Site
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'status', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'integer'],
            [['name', 'domain'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/article', 'Site');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Site::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id'         => $this->id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'created_by' => $this->created_by,
            'updated_by' => $this->updated_by,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'domain', $this->domain]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        $statusArray = self::statusArray();

        return [

            [
                'attribute' => 'id',
                'width' => '100',
            ],

            [
                'attribute' => 'name',
                'width' => '200',
            ],

            [
                'attribute' => 'domain',
                'format' => 'raw',
                'value' => function ($model, $index) {
                    return Html::a($model->domain, $model->domain, [
                        'target' => '_blank',
                    ]);
                }
            ],

            [
                'attribute' => 'status',
                'value' => function($model) use ($statusArray) {
                    return Html::tag('span', ArrayHelper::getValue($statusArray, $model->status), [
                        'class' => 'label label-info'
                    ]);
                },
                'format' => 'html',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => $statusArray,
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => Yii::t('backend/article', 'Select site status')],
            ],

            [
                'attribute' => 'created_at',
                'format'    => ['date', 'php:Y-m-d H:i:s'],
                'filterType' => GridView::FILTER_DATETIME,
            ],

            [
                'attribute' => 'updated_at',
                'format'    => ['date', 'php:Y-m-d H:i:s'],
                'filterType' => GridView::FILTER_DATETIME,
            ],

            [
                'class' => ActionColumn::className(),
                'template' => '{manage} {update} {delete}',
                'width' => '180px',
                'buttons' => [
                    'manage' => function ($url, $model) {
                        return Html::a('管理此站点', $url, [
                            'class' => 'text-danger',
                            'data-pjax' => 0
                        ]);
                    }
                ],
            ],
        ];
    }
}
